package com.tencent.mlvb.apilive.ui;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.PixelFormat;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.tencent.mlvb.apilive.R;
import com.tencent.mlvb.apilive.bean.EndLiveEvent;
import com.tencent.mlvb.apilive.bean.IsLiveEvent;
import com.tencent.mlvb.apilive.bean.StartLiveEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class PopupService extends Service {
    private WindowManager windowManager;
    private View popupView;
    private boolean isLive;
    private TextView tvContent;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e("PopupService","onStartCommand");
        if (intent != null) {
            isLive = intent.getBooleanExtra("isLive",false);
            // 使用data
            if (isLive){
                tvContent.setText("是否确认结束直播");
            }else {
                tvContent.setText("是否确认开启直播");
            }
        }
        return START_STICKY;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        // 在这里创建弹窗的视图
        popupView = LayoutInflater.from(this).inflate(R.layout.popup_view, null);
        // 设置弹窗的位置等属性
        WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, // 支持在其他App上方显示
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSPARENT);
        params.gravity = Gravity.CENTER;
        tvContent =  (TextView)popupView.findViewById(R.id.tv_content);
        TextView tvCancel =  (TextView)popupView.findViewById(R.id.tv_cancel);
        TextView tvSubmit =  (TextView)popupView.findViewById(R.id.tv_submit);
        Log.e("PopupService","isLive:"+isLive);
        if (isLive){
            tvContent.setText("是否确认结束直播");
        }else {
            tvContent.setText("是否确认开启直播");
        }
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    stopSelf();

            }
        });
        tvSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //确定
                if (isLive){
                    EventBus.getDefault().post(new EndLiveEvent());
                }else {
                    EventBus.getDefault().post(new StartLiveEvent());
                }
                stopSelf();
            }
        });
        windowManager.addView(popupView, params);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e("PopupService","onDestroy:");
        if (popupView != null && windowManager != null) {
            windowManager.removeView(popupView);
        }
    }
}